package com.lyl.quartz.test;

import com.lyl.quartz.jobs.HelloDataJob;
import com.lyl.quartz.service.HelloService;
import org.junit.jupiter.api.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

/**
 * 依赖注入。
 * @author lylbell
 * @date 2022-03-29 10:35
 */
@SpringBootTest
public class _06_QuartzTest_Data_SpringBean {


    @Test
    public void context() throws SchedulerException, InterruptedException {
        // 调度器工厂创建默认调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // 启动调度器
        scheduler.start();

        // 创建job
        JobDetail job = JobBuilder.newJob(HelloDataJob.class)
                .withIdentity("job1", "group1")
                .usingJobData("job-key-1","job-value-1")
                .usingJobData("name","lyl")
                .build();

        // 创建trigger。withIntervalInSeconds：每5秒执行一次。
        // repeatForever：一直重复执行
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .usingJobData("trigger-key-1","trigger-value-1")
                .usingJobData("name","wyx")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();

        scheduler.scheduleJob(job,trigger);


        // 执行20秒
        TimeUnit.SECONDS.sleep(1);

        scheduler.shutdown();
    }
}
